Mock.mock('/getData','get',function(){

    return Mock.mock({
        'students|1-5':[
            {
                'id|+1':1,
                'name':'@cname',
                'age|20-30':1,
                'gender':/[男女]/
            }
        ]
    })
})
Mock.mock('/addData','post',function(options){
    // console.log("options:",options);
    let {name,age} = options.body;
    console.log(name,age);
    return {
        msg:"增加成功"
    }
});

Mock.mock('/login','post',function(options){
    let {username,pwd} = options.body;
    let users = JSON.parse(localStorage.getItem("users"));
    if(users.find(item => item.username == username && item.pwd == pwd)){
        return {
            status:1
        }
    }else{
        return {
            status:0
        }
    }

});

Mock.mock('/reg','post',function(options){
    console.log(options.body);
    let {username,pwd} = options.body;
    let users = localStorage.getItem("users");
    if(!users){
        users = [];
    }else{
        users = JSON.parse(users);
    }
    users.push({username,pwd});
    localStorage.setItem("users",JSON.stringify(users));
    return {
        msg:"成功"
    }
});


Mock.mock('/usernameExist','post',function(options){
    let {username} = options.body;
    if(!localStorage.getItem("users")){
        return {
            status:1
        }
    }
    let users = JSON.parse(localStorage.getItem("users"));
    if(users.find(item => item.username == username)){
        return {
            status:0
        }
    }else{
        return {
            status:1
        }
    }
    
});

Mock.mock("/findStudents",'post',function(options){
    if(typeof options.body == "string"){
        options.body = JSON.parse(options.body);
    }
    let type = options.body.type;
    let value = options.body.value;
    console.log("options.body:",typeof options.body,"type:",type,"value:",value);
    let students = JSON.parse(localStorage.getItem("students") || "[]");
    if(type){
        students = students.filter(item => item[type].toString().search(value) >= 0);
    }
    return {
        code:0,
        data:students
    };
});

Mock.mock('/addStudent','post',function(options){
    let {name,gender,age} = options.body;
    let students = JSON.parse(localStorage.getItem('students') || '[]');
    let id = 1;
    if(students.length > 0){
        id = students[students.length - 1].id + 1;
    }
    students.push({
        id,name,gender,age
    });
    localStorage.setItem('students',JSON.stringify(students));
    return {
        msg:"增加成功"
    }
});
Mock.mock('/delStudent','post',function(options){
    let {id} = options.body;
    let students = JSON.parse(localStorage.getItem("students") || "[]");
    students = students.filter(item => item.id != id);
    localStorage.setItem('students',JSON.stringify(students));
    return {
        msg:"删除成功"
    }
});
Mock.mock('/findById','post',function(options){
    let {id} = options.body;
    let students = JSON.parse(localStorage.getItem("students") || "[]");
    console.log(id,students);
    let student = students.find(item => item.id == id);
    
    return {
        student
    }
});
Mock.mock('/updateStudent','post',function(options){
    let {id,name,gender,age} = options.body;
    let students = JSON.parse(localStorage.getItem("students") || "[]");
    let student = students.find(item => item.id == id);
    student.name = name;
    student.age = age;
    student.gender = gender;
    localStorage.setItem("students",JSON.stringify(students));
    return {
        msg:"修改成功"
    }
});